package uvt.map.graph;

import java.util.ArrayList;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import uvt.database.Database;
import uvt.database.SelectOperations;
import uvt.map.JMapViewer;
import uvt.map.graph.element.Segment;
import uvt.map.graph.element.Vehicle;

public class Graph implements Drawable {
	
	private ArrayList<Node> listNodes;
	private int indexListNodes;
	private ArrayList<FromWithOnTo> listVertex;
	private ArrayList<FromWithOnTo> listVertexForOneRoute;
	private ArrayList<Vehicle> listVehicle;
	
	public Graph() {
		listNodes = new ArrayList<Node>();
		indexListNodes = 0;
		listVertex = new ArrayList<FromWithOnTo>();
		listVertexForOneRoute = new ArrayList<FromWithOnTo>();
		listVehicle = new ArrayList<Vehicle>();		
		createGraph();
	}
	
	private void createGraph() {
		addStationsAndVertexToGraph();
		getVehicles();
	}
	
	private void addStationsAndVertexToGraph() {
		TreeMap<String,String> listRouteNameAndType = new TreeMap<String,String>();
		ArrayList<Integer> listIdRoad = new ArrayList<Integer>();
		((SelectOperations)Database.getInstace()).selectRouteNameAndType(listRouteNameAndType);
		Set<Entry<String,String>> listRouteName = listRouteNameAndType.entrySet();
		for(Entry<String,String> routeObj : listRouteName) { // pentru fiecare ruta
			int idRoute = ((SelectOperations)Database.getInstace()).selectIdRouteName(routeObj.getKey());
			listIdRoad.clear();
			((SelectOperations)Database.getInstace()).selectRoute(idRoute,listIdRoad);
			for(Integer id : listIdRoad) { // pentru fiecare drum
				FromWithOnTo obj = ((SelectOperations)Database.getInstace()).selectRoad(id);
				obj.getFrom().addNodeInNextList(obj.getTo());
				obj.getWithAndOn().getWith().setType(routeObj.getValue());
				obj.getWithAndOn().getWith().setRoute(routeObj.getKey());
				listVertex.add(obj);
				if(listNodesContainsStation(obj.getFrom().getName())) {
					listNodes.get(indexListNodes).addCoordinate(routeObj.getKey(),obj.getFrom().getCoordinate());
				} else {
					listNodes.add(obj.getFrom());
				}
				if(listNodesContainsStation(obj.getTo().getName())) {
					listNodes.get(indexListNodes).addCoordinate(routeObj.getKey(),obj.getTo().getCoordinate());
				} else {
					listNodes.add(obj.getTo());
				}
			}
		}
		
	}
	
	private void getVehicles() {
		((SelectOperations)Database.getInstace()).selectVehicles(listVehicle);
	}
	
	private boolean listNodesContainsStation(String name) {
		boolean b = false;
		indexListNodes = 0;
		for(Node node : listNodes) {
			if(node.getName().equals(name)) {
				b = true;
				return b;
			}
			indexListNodes++;				
		}
		return b;
	}
	
	public ArrayList<Segment> getListSegmentsForRoute(String routeName) {
		ArrayList<Segment> listSeg = new ArrayList<Segment>();
		ArrayList<Integer> listIdRoad = new ArrayList<Integer>();
		int idRouteName = ((SelectOperations)Database.getInstace()).selectIdRouteName(routeName);
		((SelectOperations)Database.getInstace()).selectRoute(idRouteName, listIdRoad);
		listVertexForOneRoute.clear();
		for(Integer i : listIdRoad) {
			listVertexForOneRoute.add(((SelectOperations)Database.getInstace()).selectRoad(i));
		}
		for(FromWithOnTo obj : listVertexForOneRoute) {
			for(Segment s : obj.getWithAndOn().getStreet().getListSegments()) {
				listSeg.add(s);
			}
		}
		return listSeg;
	}
	
	public void draw(JMapViewer map) {
		for(FromWithOnTo obj : listVertex) {
			obj.draw(map);
		}
	}

	public ArrayList<Node> getListNodes() {
		return listNodes;
	}

	public ArrayList<FromWithOnTo> getListVertex() {
		return listVertex;
	}
	
	public  ArrayList<Vehicle> getListVehicle() {
		return listVehicle;
	}
	
	public void setListVehicle(ArrayList<Vehicle> list) {
		listVehicle.clear();
		listVehicle = list;
	}

}
